import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/fingerprint/widgets/add_fingerprint_tip/index.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/show_datetime_picker.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

enum UsageTimeType {
  usage,
  forever,
}

class TabPane extends StatefulWidget {
  final UsageTimeType usageTimeType;
  final Map info;
  const TabPane({super.key, required this.usageTimeType, required this.info});

  @override
  State<StatefulWidget> createState() => _LimitedState();

}

class _LimitedState extends State<TabPane> {
  final FocusNode _nameFocus = FocusNode();
  final DateTime currentDate = DateTime.now();
  final Map _params = {
    "fingerprintName": "",
    "startDate": DateTime.now(),
    "endDate": DateTime.now(),
  };

  bool _canAdd() {
    bool timePass = false;
    switch (widget.usageTimeType) {
      case UsageTimeType.usage:
        var startDate = _params["startDate"] is DateTime ? _params["startDate"].millisecondsSinceEpoch : DateTime.parse(_params["endDate"]).millisecondsSinceEpoch;
        var endDate = _params["startDate"] is DateTime ? _params["endDate"].millisecondsSinceEpoch : DateTime.parse(_params["endDate"]).millisecondsSinceEpoch;

        if (startDate != endDate && endDate > startDate) {
          timePass = true;
        }
        break;
      case UsageTimeType.forever:
        timePass = true;
        break;
    }
    return Utils.isNotEmpty(_params["fingerprintName"]) && timePass;
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: const EdgeInsets.all(ThemeVars.paddingPage),
      children: [
        MenuBlock(
          items: [
            MenuBlockItem(
              text: "姓名",
              showArrow: false,
              suffix: TextField(
                focusNode: _nameFocus,
                textAlign: TextAlign.right,
                decoration: const InputDecoration(
                  hintText: "请输入指纹名称",
                  hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400),
                  isDense: true,
                ),
                onChanged: (value) => setState(() {
                  _params["fingerprintName"] = value;
                }),
                onTapOutside: (event){
                  _nameFocus.unfocus();
                },
              )
            ),
          ]
        ),
        const SizedBox(height: 12,),
        if (widget.usageTimeType == UsageTimeType.usage) MenuBlock(
          items: [
            MenuBlockItem(
              text: "生效时间",
              showArrow: true,
              suffix: Text(
                DateTools.formatYMD(_params["startDate"], format: "{y}-{m}-{d} {h}:{i}"),
                style: const TextStyle(color: ThemeVars.colorTextTer),
              ),
              onTap: () {
                showDatetimePicker(
                  context: context,
                  initialDateTime: _params["startDate"],
                  onchange: (value) => setState(() {
                    print(value);
                    if (value.isAfter(_params["endDate"])) {
                      EasyLoading.showToast("生效时间不能大于失效时间");
                    } else {
                      _params["startDate"] = value;
                    }

                  }),
                );
              },
            ),
            MenuBlockItem(
              text: "失效时间",
              showArrow: true,
              suffix: Text(
                DateTools.formatYMD(_params["endDate"], format: "{y}-{m}-{d} {h}:{i}"),
                style: const TextStyle(color: ThemeVars.colorTextTer),
              ),
              onTap: () {
                showDatetimePicker(
                  context: context,
                  initialDateTime: _params["endDate"],
                  onchange: (value) => setState(() {
                    if (value.isBefore(_params["endDate"])) {
                      EasyLoading.showToast("失效时间不能小于生效时间");
                    } else {
                      _params["endDate"] = value;
                    }
                  }),
                );
              },
            ),
          ]
        ),
        const SizedBox(height: 12,),
        Padding(
          padding: const EdgeInsets.only(top: 60),
          child: Button(
            radius: 5,
            type: ButtonType.primary,
            onPressed: _canAdd() ? () {
              Map params = {
                "fingerprintName": _params["fingerprintName"],
              };
              if (widget.usageTimeType == UsageTimeType.usage) {
                params["startDate"] = DateTools.formatYMD(_params["startDate"], format: "{y}-{m}-{d} {h}:{i}:{s}");
                params["endDate"] = DateTools.formatYMD(_params["endDate"], format: "{y}-{m}-{d} {h}:{i}:{s}");
              }
              Stacker.pushNamed(Keys.DEVICE_LOCK_FINGERPRINT_ADD_TIP, arguments: {"info": widget.info, "params": params});
            } : null,
            child: const Text("确定"),
          ),
        )
      ],
    );
  }

}